/**
 * JimBot - Java IM Bot
 * Copyright (C) 2006-2009 JimBot project
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ru.jimbot.protocol;

import com.obimp.OBIMPConnection;
import org.jivesoftware.smack.XMPPConnection;
import ru.caffeineim.protocols.icq.core.OscarConnection;
import ru.caffeineim.protocols.icq.integration.OscarInterface;
import ru.caffeineim.protocols.icq.setting.enumerations.ClientModeEnum;
import ru.caffeineim.protocols.icq.setting.enumerations.StatusModeEnum;
import ru.caffeineim.protocols.icq.setting.enumerations.XStatusModeEnum;
import ru.jimbot.modules.AbstractProps;
import ru.jimbot.modules.MsgOutQueue;
import ru.jimbot.util.Log;
import ru.jimbot.util.MainProps;

/**
 * Реализация действий протокола
 * @author Prolubnikov Dmitry
 */
public abstract class AbstractProtocol {
    
    /**
     * Определяем параметры для подключения
     */
    
    // ICQ
    public String server="";
    public int port = 0;
    public boolean useProxy = false;
    public String proxyHost = "";
    public int proxyPort = 0;
    public String proxyUser = "";
    public String proxyPass = "";
    
    // XMPP (Jabber)
    public String server_1=MainProps.getStringProperty("jabber.serverDefault");
    public int port_1 = MainProps.getIntProperty("jabber.portDefault");
    public boolean useProxy_xmpp = false;
    public String proxyHost_xmpp = "";
    public int proxyPort_xmpp = 0;
    public String proxyUser_xmpp = "";
    public String proxyPass_xmpp = "";
    
    public String screenName = "";
    public String password = "";
    public String serviceName = "";
    public String type = "";
    public String NickName, FirstName, LastName, About;
    public MsgOutQueue mq;
    public String baseUin="";
    private String basePass="";  
    public boolean connected = false;
    private AbstractProps props;
    public OscarConnection con = null;
    public XMPPConnection con_xmpp = null;
    public OBIMPConnection con_obimp = null;
    public String lastInfo = "";
    int xStatusId;
    String xStatusText;
    
    protected ProtocolListener protList;
    
    /**
     * Основные методы
     */
    public void addListener(ProtocolListener p){
        protList = p;
        this.props = props;
    }
    
    public abstract void connect();
    public abstract void reConnect();
    public abstract void disconnect();
    public abstract void setStatus(int status);
    public abstract void sendMsg(String sn, String msg);
    public abstract void getMsg(String sendSN, String recivSN, String msg, boolean isOffline);
    public abstract void getStatus(String sn, int status);
    public abstract void addContactList(String sn);
    public abstract void RemoveContactList(String sn);
    /////////////////////////////////////////////////
    public abstract AbstractProps getProps();
    public abstract boolean userInfoRequest(String paramString1, String paramString2);
    public abstract void authRequest(String paramString1, String paramString2);
    public abstract int getOuteqSize();
    /////////////////////////////////////////////////////
    
    /**
    * Для Автосмены X-Статуса
    * @param number
    */
    public void setXStatusNumber( int number ){
        setStatus(number);
    }

   
   
    public void setXStatus( int n, String text ){
        if ( n >= 0 && n <= 37 ){
            xStatusId = n;
            props.setStringProperty( "icq.xstatus", Integer.toString(xStatusId) );
        }
        if ( !text.equals( "" ) ){
            xStatusText = text;
            props.setStringProperty("icq.STATUS_MESSAGE2", xStatusText);
        }
        int client=Integer.parseInt(props.getStringProperty("icq.client"));
        OscarInterface.changeXStatus(con, new XStatusModeEnum(xStatusId), new ClientModeEnum(client));
    }
   
    public boolean isNoAuthUin(String uin) {
        return props.getBooleanProperty("chat.isAuthRequest");
    }
   
    public boolean isOnLine() {
        if(con == null) return false;
        return connected;
    }
   
    public void onLogin() {
        connected = true;
        OscarInterface.changeStatus(con, new StatusModeEnum(Integer.parseInt(props.getStringProperty("icq.status")), props.getBooleanProperty("ball.of.joy.on.off")));
        OscarInterface.changeXStatus(con, new XStatusModeEnum(Integer.parseInt(props.getStringProperty("icq.xstatus"))), new ClientModeEnum(Integer.parseInt(props.getStringProperty("icq.client"))));
        Log.info("UIN - " + screenName + " online");
        if(!props.getBooleanProperty("chat.IgnoreOfflineMsg")) OscarInterface.requestOfflineMessages(con);
    }
}